# toolchain
#########################################包含子makefile
include board/makefile_board.mk 
include motorControl/makefile_motorcontrol.mk 
include drivers/makefile_drivers.mk 
include communication/makefile_communication.mk
########################################定义编译工具
COMPILE       = arm-none-eabi-g++
COMPILE_CCC   = arm-none-eabi-gcc -std=c99
COMPILE_CXX   = arm-none-eabi-g++ -std=c++17 -Wno-register
COMPILE_ASM   = arm-none-eabi-gcc -x assembler-with-cpp 
COMPILE_AS    = arm-none-eabi-as
COMPILE_LD    = arm-none-eabi-ld
COMPILE_AR    = arm-none-eabi-ar
COMPILE_OBJCP = arm-none-eabi-objcopy
#########################################定义文件格式和文件名
PROJECT_NAME             = GD_driver
TARGET                  := $(PROJECT_NAME)
TARGET_ELF              := $(TARGET).elf
TARGET_BIN              := $(TARGET).bin
TARGET_HEX              := $(TARGET).hex
HEX          = arm-none-eabi-objcopy -O ihex
BIN          = arm-none-eabi-objcopy -O binary -S
BIN_TO_HEX   = -I binary -Oihex
#########################################定义模块路径
ROOT_DIR          = .
BOARD_DIR         = $(ROOT_DIR)/board
MOTORCONTROL_DIR  = $(ROOT_DIR)/motorControl
DRIVERS_DIR       = $(ROOT_DIR)/drivers
COMMUNICATION_DIR = $(ROOT_DIR)/communication
#########################################定义模块头文件路径
INCLUDE_DIRS += $(BOARD_INCLUDE_DIRS)
INCLUDE_DIRS += $(MOTORCONTROL_INCLUDE_DIRS)
INCLUDE_DIRS += $(DRIVERS_INCLUDE_DIRS)
INCLUDE_DIRS += $(COMMUNICATION_INCLUDE_DIRS)
#########################################编译生成文件夹
CCC_DIR = $(BUILD_DIR)/C
CPP_DIR = $(BUILD_DIR)/CPP
ASM_DIR = $(BUILD_DIR)/ASM
BUILD_DIR    = build
#########################################链接.ld文件
LINK_SCRIPT  = $(BOARD_DIR)/ldscripts/STM32F405RGTx_FLASH.ld
#########################################编译标志位
FLAGS += -DHW_VERSION_MAJOR=3
FLAGS += -DHW_VERSION_MINOR=6
FLAGS += -DHW_VERSION_VOLTAGE=24
FLAGS += -DUSB_PROTOCOL_NATIVE
FLAGS += -DUART_PROTOCOL_ASCII
FLAGS += -Werror
####C-编译-标志位
FLAGS += -D__weak="__attribute__((weak))"
FLAGS += -D__packed="__attribute__((__packed__))"
FLAGS += -DUSE_USB_FS
FLAGS += -DGD32F405

FLAGS += -mthumb
FLAGS += -mcpu=cortex-m4
FLAGS += -mfpu=fpv4-sp-d16
FLAGS += -mfloat-abi=hard
FLAGS += -Wall
FLAGS += -Wdouble-promotion
FLAGS += -Wfloat-conversion
FLAGS += -fdata-sections
FLAGS += -ffunction-sections
FLAGS += -w
####--链接-标志位
FLAGS += -g -gdwarf-2
OPT += -Og
####--公共ASM,C,C++
OPT += -ffast-math
OPT += -fno-finite-math-only
####--链接-标志位
LDFLAGS += -T$(LINK_SCRIPT)
#LDFLAGS += -L $(DRIVERS_CMSIS_LIB_DIR)
#LDFLAGS += '-lc -lm -lnosys -larm_cortexM4lf_math'
LDFLAGS += -mthumb -mcpu=cortex-m4 -mfpu=fpv4-sp-d16 -specs=nosys.specs -specs=nano.specs -u _printf_float -u _scanf_float -Wl,--cref -Wl,--gc-sections
LDFLAGS += -Wl,--undefined=uxTopUsedPriority
LDFLAGS += -Wl,-Map=$(BUILD_DIR)/$(TARGET).map


#########################################需要链接的静态库
LDLIBS += $(BOARD_DIR)/Firmware/CMSIS/arm_math/libarm_cortexM4lf_math.a
#########################################需要编译的 .C 代码
CCC_SRC		+= $(BOARD_CCC_SRC)
CCC_SRC 	+= $(DRIVERS_CCC_SRC)
CCC_SRC   	+= $(MOTORCONTROL_CCC_SRC)

#########################################需要编译的 .cpp 代码
CXX_SRC   	+= $(MOTORCONTROL_CXX_SRC)
CXX_SRC 	+= $(COMMUNICATION_CXX_SRC)
#########################################需要编译的 .ams 代码
ASM_SRC   += $(BOARD_ASM_SRC) 
#########################################所有头文件
INC_DIR  = $(patsubst %, -I%, $(INCLUDE_DIRS))
#########################################所有需要编译的文件
# list of c program objects
OBJECTS = $(addprefix $(CCC_DIR)/,$(notdir $(CCC_SRC:.c=.o)))
vpath %.c $(sort $(dir $(CCC_SRC)))
# list of ASM program objects
OBJECTS += $(addprefix $(ASM_DIR)/,$(notdir $(ASM_SRC:.s=.o)))
vpath %.s $(sort $(dir $(ASM_SRC)))
# list of cpp program objects
OBJECTS += $(addprefix $(CPP_DIR)/,$(notdir $(CXX_SRC:.cpp=.o)))
vpath %.cpp $(sort $(dir $(CXX_SRC)))

AS_FLAGS  = $(FLAGS) $(OPT) 
CC_FLAGS  = $(FLAGS) $(OPT) 
CXX_FLAGS = $(FLAGS) $(OPT) 

#########################################编译
all: $(OBJECTS) $(BUILD_DIR)/$(PROJECT_NAME).elf  $(BUILD_DIR)/$(PROJECT_NAME).hex $(BUILD_DIR)/$(PROJECT_NAME).bin
	
	 
$(CCC_DIR)/%.o: %.c Makefile | $(BUILD_DIR)
	@$(COMPILE_CCC) -c $(CC_FLAGS) -I. $(INC_DIR) $< -o $@
	@echo -e "\033[40;32m builded ======== $@ \033[0m" 
	
$(ASM_DIR)/%.o: %.s Makefile | $(BUILD_DIR)
	@$(COMPILE_ASM) -c $(AS_FLAGS) $< -o $@
	@echo -e "\033[40;32m builded ======== $@ \033[0m" 
	
$(CPP_DIR)/%.o: %.cpp Makefile | $(BUILD_DIR)
	@$(COMPILE_CXX) -c $(CXX_FLAGS) -I. $(INC_DIR) $< -o $@
	@echo -e "\033[40;32m builded ======== $@ \033[0m" 
	
$(BUILD_DIR)/%.elf: $(OBJECTS)  Makefile
	@$(COMPILE) $(OBJECTS) $(LDFLAGS) $(FLAGS) $(LDLIBS) -o $@
	@echo -e "generate ======== $@" 
$(BUILD_DIR)/%.hex: $(BUILD_DIR)/%.elf | $(BUILD_DIR)
	$(HEX) $< $@
	
$(BUILD_DIR)/%.bin: $(BUILD_DIR)/%.elf | $(BUILD_DIR)
	$(BIN)  $< $@

$(BUILD_DIR):
	mkdir $@
	mkdir $(BUILD_DIR)/C
	mkdir $(BUILD_DIR)/CPP
	mkdir $(BUILD_DIR)/ASM


	
clean:
	-rm -rf $(BUILD_DIR)

